<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="redis" uri="http://www.shopjsp.com/redisTag/functions" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${redis:getSysConfig('base_path_back')}"/>
<c:set var="uploadPath" value="${redis:getSysConfig('uploadFileVisitRoot')}"/>
<script type="text/javascript">
    /**
     * 自定义验证规则
     */
    function validateForm(){
        form.bootstrapValidator({
            message: "无效的值",
            feedbackIcons: {
                valid: "glyphicon glyphicon-ok",
                invalid: "glyphicon glyphicon-remove",
                validating: "glyphicon glyphicon-refresh"
            },
            fields: {
                imageInfo_1: {
                    validators: {
                        file:{maxSize: 1024*1024, message: '请上传一张最大不超过1M的jpg/png/gif图标文件'}
                    }
                },
                purviewName: {
                    validators: {
                        notEmpty: {message: "权限名称不可为空"},
                        stringLength: {min: 2,max: 30,message: "权限名称长度为2至30"},
                        regexp: {
                            regexp: validateUtil.chinese,
                            message: '权限名称为汉字'
                        }
                    }
                },
				url: {
					validators: {
						notEmpty: {message: "权限路径不可为空"},
						stringLength: {max: 200,message: "权限路径最大长度为200"}
					}
				},
                purviewValidateCode: {
                    validators: {
                        notEmpty: {message: "权限验证编码不可为空"},
                        stringLength: {max: 50,message: "权限验证编码最大长度为50"},
                        regexp: {
                            regexp: validateUtil.onlyChar,
                            message: '权限验证编码只能为字母'
                        },
                        threshold :  1 ,
                        /**bootstrap validator 远程校验需要返回{"valid",true or false}**/
                        remote: {
                            url:'${basePath}/back/purview/checkPurviewValidateCode.sjson',
                            message:  '编码已存在',
                            delay:  20000,
                            type:  'POST',
                            data: function(validator) {
                                return {
                                    purviewValidateCode:$("#purviewValidateCode").val(),
                                    purviewId:$("#purviewId").val()
                                };
                            }
                        }
                    }
                },
                sortCode: {
                    validators: {
                        notEmpty: {message: "排序号不可为空"},
                        numeric: {
                            message: '排序号只能输入数字'
                        },
                        stringLength: {max: 8,message: "排序号最大长度为8"}
                    }
                }
            }
        });
    }

    /**
     * 添加分类名称
     */
	<shiro:hasPermission name="back_gotoPurviewPage_add">
    function addInfo(){
        $("#myModalLabel").empty().text("添加权限信息");
        /**清空隐藏域的值**/
        $("#purviewId").val("");
        $("#isLeaf").val("");
        $("#levelCode").val("");
        $("#imageUrl_1").val("");
        /**设置固定隐藏域的值  通过左侧的树节点点击展示右侧的子节点列表，则节点的id为列表的子节点的父id**/
        $("#parentId").val($("#q_purviewId").val());
        /**如果有上传图片功能添加此代码，否则可以去掉**/
        initImageForAdd(1,"sys_purview","image_purview",1,50,50);
        /**重置可见表单中的数据**/
        resetAddForm();
        /**当前查询列表菜单的级别**/
        var allRowData=$("#tt").bootstrapTable('getData');
        currentLevelCode=allRowData[0].levelCode;
        if(currentLevelCode==1||currentLevelCode==2){
            $("#purviewValidateCodeTr").hide();
            $("#url").val("#").attr("readonly","readonly");
        }else{
            $("#purviewValidateCodeTr").show();
            $("#url").val().removeAttr("readonly");
        }
    }
	</shiro:hasPermission>

    /**
     * 编辑分类名称
     */
	<shiro:hasPermission name="back_gotoPurviewPage_update">
    function editInfo(){
        $("#myModalLabel").empty().text("修改权限信息");
        /**获取选中行的id**/
        var selectedRow= $("#tt").bootstrapTable('getSelections');
        if(selectedRow.length==1){
            var id=selectedRow[0].purviewId;
            $("#btn_edit").attr("data-target","#addOrEditModal");/**选中了记录后才可打开对应的模态框**/
            $.ajax({
                type: "POST",
                dataType: "JSON",
                url: "${basePath}/back/purview/getPurviewObject.sjson",
                data: {purviewId:id},
                success: function(result){
                    var data = JSON.parse(result.data);
                    /**当前查询列表菜单的级别**/
                    var currentLevelCode=selectedRow[0].levelCode;
                    if(currentLevelCode==1||currentLevelCode==2){
                        $("#purviewValidateCodeTr").hide();
                        $("#url").val("#").attr("readonly","readonly");
                    }else{
                        $("#purviewValidateCodeTr").show();
                        $("#purviewValidateCode").val(data.purviewValidateCode);
                        $("#url").val(data.url).removeAttr("readonly");
                    }
                    /**隐藏域**/
                    $("#purviewId").val(data.purviewId);
                    $("#parentId").val(data.parentId);
                    $("#oldParentId").val(data.parentId);
                    $("#imageUrl_1").val(data.iconUrl);
                    $("#isLeaf").val(data.isLeaf);
                    $("#levelCode").val(data.levelCode);
                    /**可见表单控件**/
                    $("#purviewName").val(data.purviewName);
                    $("#sortCode").val(data.sortCode);

                    /**回写预览上传文件所需的信息，如果有上传图片功能添加此代码，否则可以去掉**/
                    initImageForUpdate(1,"sys_purview","image_purview",1,50,50,"${uploadPath}",data.iconUrl);
                    reValidateForm();
                    removeValidateField('imageInfo_1');
                }
            });
        }else{
            $("#btn_edit").attr("data-target","");/**没有选中记录不可打开模态框**/
            showMsg("请选择一行记录！");
        }
    }
	</shiro:hasPermission>
</script>

<!--添加编辑模态框（Modal） -->
<div class="modal fade" id="addOrEditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
	<div class="modal-dialog" style="width:70%">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h5 class="modal-title" id="myModalLabel"></h5>
			</div>
			<div class="modal-body">
				<form id="form1"  method="post" action="${basePath}/back/purview/saveOrUpdatePurview.sjson">
					<input id="purviewId" type="hidden" name="purviewId" value=""/>
					<input id="parentId" type="hidden" name="parentId"/>
					<input id="isLeaf" type="hidden" name="isLeaf" value=""/>
					<input id="levelCode" type="hidden" name="levelCode" value=""/>

					<table align="center" class="addOrEditTable table-bordered table" style="width:100%;">
						<tr>
							<td class="toright_td" width="150px">权限图标:</td>
							<td class="toleft_td">
								<div class="container">
									<input id="imageUrl_1" type="hidden" name="iconUrl" value=""/>
									<div class="form-group" id="fileWrap1"></div>
								</div>
								<span style="color:red">提示：请上传规格宽50px，高50px的图片</span>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>权限名称:&nbsp;&nbsp;</td>
							<td  class="toleft_td">
								<div class="form-group">
									<input id="purviewName" class="form-control input-sm" type="text" name="purviewName" placeholder="请输入权限名称"/>
								</div>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>权限路径:&nbsp;&nbsp;</td>
							<td  class="toleft_td">
								<div class="form-group">
									<input id="url" class="form-control input-sm" type="text" name="url" placeholder="请输入权限路径"/>
								</div>
							</td>
						</tr>
						<tr id="purviewValidateCodeTr">
							<td class="toright_td" width="150px"><span style="color:red">* </span>权限验证编码:&nbsp;&nbsp;</td>
							<td  class="toleft_td">
								<div class="form-group">
									<input id="purviewValidateCode" class="form-control input-sm" type="text" name="purviewValidateCode" placeholder="请输入权限验证编码"/>
								</div>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>排序:&nbsp;&nbsp;</td>
							<td  class="toleft_td">
								<div class="form-group">
									<input id="sortCode" class="form-control input-sm" type="text" name="sortCode" placeholder="请输入排序"/>
								</div>
							</td>
						</tr>
					</table>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" id="validateBtn" class="btn btn-primary"  onclick="submitFormByRemote()">保存</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
			</div>

		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>
